package com.bulletelc.apollopre.module.display.product.details.comment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.manager.imageload.ImageLoaderManager;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * 图片查看器界面中,显示单张图片的fragment
 * Created by Administrator on 2017/3/20 0020.
 */

public class ImageDetailFragment extends Fragment implements PhotoViewAttacher.OnPhotoTapListener {
    private PhotoView photoView;
    private PhotoViewAttacher attacher;
    private String path;
    private static final String ARGS_PATH = "args_path";

    public static ImageDetailFragment newInstance(String path) {
        ImageDetailFragment fragment = new ImageDetailFragment();
        Bundle args = new Bundle();
        args.putString(ARGS_PATH, path);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        path = getArguments() != null ? getArguments().getString(ARGS_PATH) : null;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.detail_image_fragment, container, false);
        photoView = view.findViewById(R.id.photoView);
        attacher = new PhotoViewAttacher(photoView);
        attacher.setOnPhotoTapListener(this);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ImageLoaderManager.getInstance().loadImage(
                ImageLoaderManager.getDefaultOptions(photoView, path)
        );
    }

    @Override
    public void onPhotoTap(View view, float x, float y) {
        getActivity().finish();
    }
}
